package utils.parser
{
	import flash.display3D.Context3D;
	import flash.display3D.IndexBuffer3D;
	import flash.display3D.VertexBuffer3D;
	
	import utils.base.Joint;
	import utils.base.Mesh;
	import utils.base.MeshBuffer;

	public class Stage3d_md5_parser extends Stage3DParser
	{
		
		private var vertexFrame:Vector.<Vector.<Number>>;
		
		private var mesh:Stage3D_md5mesh_parser;
		private var anim:Stage3D_md5anim_parser;
		
		protected var _indexBuffer:IndexBuffer3D;
		protected var positionsBufferAry:Vector.<VertexBuffer3D>;
		protected var _uvBuffer:VertexBuffer3D;
		
		private var _context3d:Context3D;
		public var currentFrame:int;
		private var meshAry:Vector.<Mesh>;
		private var _meshList:Vector.<MeshBuffer>;
		public function Stage3d_md5_parser(mesh:Stage3D_md5mesh_parser,anim:Stage3D_md5anim_parser,contex3d:Context3D)
		{
			super();
			this.anim = anim;
			this.mesh = mesh;
			this._context3d = contex3d;
			vertexFrame = new Vector.<Vector.<Number>>;
			_meshList = new Vector.<MeshBuffer>;
			process();
		}
		private function process():void{
			anim.setBaseInfo(mesh);
			var jointAnimVecter:Vector.<Vector.<Joint>> = anim.jointAnimVecter;
			for(var i:int=0;i<jointAnimVecter.length;i++){
				mesh.getVerter(jointAnimVecter[i]);
			}
			this.meshAry = mesh.meshAry;
			updataBuffer();
		}
		private function updataBuffer():void{
			for(var i:int;i<meshAry.length;i++){
				var mesh:Mesh = meshAry[i];
				var meshBuffer:MeshBuffer = new MeshBuffer();
				for(var j:int=0;j<mesh.vertexList.length;j++){
					//var rawPositionsBuffer:Vector.<Number> = mesh.rawPositionsBuffer
					var rawPositionsBuffer:Vector.<Number> = mesh.vertexList[j];
					var vertexCount:uint = rawPositionsBuffer.length/3;
					var positionsBuffer:VertexBuffer3D = _context3d.createVertexBuffer(vertexCount, 3);
					positionsBuffer.uploadFromVector(rawPositionsBuffer, 0, vertexCount);
					meshBuffer.positionsBufferAry.push(positionsBuffer);
				}
				var uvsCount:uint = mesh.rawUvBuffer.length/2;
				_uvBuffer = _context3d.createVertexBuffer(uvsCount, 2);
				_uvBuffer.uploadFromVector(mesh.rawUvBuffer, 0, uvsCount);
				meshBuffer.uvBuffer = _uvBuffer;
				
				_indexBuffer = _context3d.createIndexBuffer(mesh.rawIndexBuffer.length);
				_indexBuffer.uploadFromVector(mesh.rawIndexBuffer, 0, mesh.rawIndexBuffer.length);
				meshBuffer.indexBuffer = _indexBuffer;
				
				meshBuffer.indexBufferCount = mesh.rawIndexBuffer.length / 3;
				
				_meshList.push(meshBuffer);
			}
		}
		private function updateVertexBuffer():void{
			positionsBufferAry = new Vector.<VertexBuffer3D>;
			for(var i:int=0;i<vertexFrame.length;i++){
				var rawPositionsBuffer:Vector.<Number> = vertexFrame[i];
				var vertexCount:uint = rawPositionsBuffer.length/3;
				var positionsBuffer:VertexBuffer3D = _context3d.createVertexBuffer(vertexCount, 3);
				positionsBuffer.uploadFromVector(rawPositionsBuffer, 0, vertexCount);
				positionsBufferAry.push(positionsBuffer);
			}
		}
		public function play():void{
			currentFrame++;
			if(currentFrame == anim.jointAnimVecter.length){
				currentFrame = 0;
			}
		}
		public function gotoAndPlay(num:int):Boolean{
			currentFrame = num;
			if(currentFrame >= anim.jointAnimVecter.length){
				currentFrame = 0;
				return false;
			}
			return true;
		}
		override public function get positionsBuffer():VertexBuffer3D
		{
			//return mesh.positionsBuffer;
			return positionsBufferAry[currentFrame];
		}
		override public function get indexBuffer():IndexBuffer3D
		{
			if(!_indexBuffer)
				updateIndexBuffer();
			return _indexBuffer;
		}
		override public function get indexBufferCount():int
		{
			return mesh.rawIndexBuffer.length / 3;
		}
		override public function get uvBuffer():VertexBuffer3D
		{
			if(!_uvBuffer)
				updateUvBuffer();
			return _uvBuffer;
		}
		
		public function updateUvBuffer():void
		{
			var uvsCount:uint = mesh.rawUvBuffer.length/2;
			_uvBuffer = _context3d.createVertexBuffer(uvsCount, 2);
			_uvBuffer.uploadFromVector(mesh.rawUvBuffer, 0, uvsCount);
		}
		
		public function updateIndexBuffer():void
		{
			_indexBuffer = _context3d.createIndexBuffer(mesh.rawIndexBuffer.length);
			_indexBuffer.uploadFromVector(mesh.rawIndexBuffer, 0, mesh.rawIndexBuffer.length);
		}

		override public function get meshList():Vector.<MeshBuffer>
		{
			return _meshList;
		}

	}
}